package com.cn.qgx428.lazydiary.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.cn.qgx428.lazydiary.Presenter.BillPresenter
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.RecordHelper
import com.cn.qgx428.lazydiary.dialog.ScrollPickerDialog
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.mWidget.Pickers
import kotlinx.android.synthetic.main.activity_bill_add.*
import org.jetbrains.anko.toast

class BillAddActivity : BaseActivity() {

    var nowType : String = "工资"
    var nowSelected : String = "收入"

    override fun initUI() {
        hindActionBar()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bill_add)
        initView()
    }

    private fun initView() {
        billAdd_income.setOnClickListener({
            billAdd_income.setTextColor(resources.getColor(R.color.colorAccent))
            billAdd_income.setBackgroundColor(resources.getColor(R.color.mWhite))
            billAdd_expend.setTextColor(resources.getColor(R.color.mWhite))
            billAdd_expend.setBackgroundColor(resources.getColor(R.color.colorAccent))

            nowSelected = "收入"
            nowType = "工资"
            billAdd_type.text = "工资"
            billAdd_typeIcon.setImageResource(RecordHelper.getBillIcon(nowSelected, nowType))
        })
        billAdd_expend.setOnClickListener({
            billAdd_expend.setTextColor(resources.getColor(R.color.colorAccent))
            billAdd_expend.setBackgroundColor(resources.getColor(R.color.mWhite))
            billAdd_income.setTextColor(resources.getColor(R.color.mWhite))
            billAdd_income.setBackgroundColor(resources.getColor(R.color.colorAccent))

            nowSelected = "支出"
            nowType = "一般"
            billAdd_type.text = "一般"
            billAdd_typeIcon.setImageResource(RecordHelper.getBillIcon(nowSelected, nowType))
        })

        billAdd_typeLayout.setOnClickListener({
            var types: List<String>?
            if (nowSelected.equals("收入")){
                types = RecordHelper.getBillIncomeTypes()
            } else{
                types = RecordHelper.getBillExpendTypes()
            }
            // 显示选择账单类型对话框
            ScrollPickerDialog(this, types,{
                picker ->
                nowType = picker.showContent
                billAdd_type.setText(nowType)
                //更改图标
                billAdd_typeIcon.setImageResource(RecordHelper.getBillIcon(nowSelected, nowType))
            }).show()
        })

        billAdd_save.setOnClickListener({
            if (checkInputInfo()){
                BillPresenter.addBill(nowSelected, nowType, billAdd_price.text.toString(),
                        billAdd_no.text.toString(), billAdd_remark.text.toString(),
                        {
                            toast("已保存")
                            LManager.newInstance().trigger(LAction.DB_UPDATE_BILL)
                            finish()
                        })
            }
        })

        billAdd_back.setOnClickListener({
            finish()
        })
    }

    /**
     * 检测输入信息
     */
    fun checkInputInfo(): Boolean{
        if (billAdd_price.text.toString().equals("") ||
                billAdd_price.text.toString().equals("0"))
        {
            toast("没输入金额哦")
            return false
        }
        return true
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.keep, R.anim.from_bottom_out)
    }

}
